package com.webservice;

import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.ParksModel;
import com.carpark.ejb.Model.PaymentsModel;
import com.carpark.ejb.Model.ReceivablesModel;
import com.carpark.ejb.Model.TarrifsModel;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Krzysztof
 */
@ManagedBean
@RequestScoped
public class TarrifsParks implements Serializable {

    private final DAO dao = getSingleDao();
    DaoSingleObject dso;

    public DAO getDao() {
        return dao;
    }

    Integer Client_Id;
    List<TarrifsModel> tarrifs;
    Map<String, List<TarrifsModel>> map;
    List<String> carsTypes;
    String selectedCarType;
    private final static String[] Entry_or_Exit = new String[2];
    private final static String[] payerTypes = new String[2];
    private SelectItem[] operationOptions;
    List<ParksModel> parksByClient;
    String parkDate;
    String operationType;
    ClientsModel currentUserProfile;
    List<ParksModel> filteredParks;
    List<ParksModel> extByClient;
    List<ParksModel> entByClient;
    LoginBean lg;
    SimpleDateFormat formatter;
    String creationDateForUser;
    String payDeadlineForUser;

    public SelectItem[] getOperationOptions() {
        return operationOptions;
    }

    public List<ParksModel> getFilteredParks() {
        return filteredParks;
    }

    public void setFilteredParks(List<ParksModel> filteredParks) {
        this.filteredParks = filteredParks;
    }

    public ClientsModel getCurrentUserProfile() {
        return dao.getClientById(Client_Id);
    }

    public DAO getSingleDao() {
        if (this.dso == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.dso = (DaoSingleObject) app.evaluateExpressionGet(context, "#{daoSingleObject}", DaoSingleObject.class);
        }
        return this.dso.dao;
    }

    public List<ParksModel> getParksByClient() {
        if (parksByClient == null) {
            this.parksByClient = dao.getParksByClient(dao.getClientById(Client_Id));

        }
        return this.parksByClient;
    }

    public String getOperationType(ParksModel p) {

        return getLabelFromValueOperation(p.getType());
    }

    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }

    public LoginBean getLg() {
        return lg;
    }

    public void setLg(LoginBean lg) {
        this.lg = lg;
    }

    public List<String> getCarTypes() {
        return carsTypes;
    }

    public List<ParksModel> getExtByClient() {
        if (extByClient == null) {
            this.extByClient = dao.getParksByClientAndType(dao.getClientById(Client_Id), "Exit");

        }
        return this.extByClient;
    }

    public void setExtByClient(List<ParksModel> extByClient) {
        this.extByClient = extByClient;
    }

    public List<ParksModel> getEntByClient() {
        if (entByClient == null) {
            this.entByClient = dao.getParksByClientAndType(dao.getClientById(Client_Id), "Entry");

        }
        return this.entByClient;
    }

    public void setEntByClient(List<ParksModel> entByClient) {
        this.entByClient = entByClient;
    }

    public Map<String, List<TarrifsModel>> getMap() {
        return map;
    }

    public String getCreationDateForUser() {

        return creationDateForUser;
    }

    public String getPayDeadline(ReceivablesModel rcv) {
        return formatter.format(rcv.getPayDeadline());
    }

    public String getParkDate(ParksModel p) {

        return formatter.format(p.getParkDate());
    }

    public void setParkDate(String parkDate) {
        this.parkDate = parkDate;
    }

    public String getCreationDate(ReceivablesModel rcv) {
        return formatter.format(rcv.getCreationDate());
    }

    public String getSelectedCarType() {
        return selectedCarType;
    }

    public void setSelectedCarType(String selectedCarType) {
        this.selectedCarType = selectedCarType;
    }

    public void setCreationDateForUser(String creationDateForUser) {
        this.creationDateForUser = creationDateForUser;
    }

    public String getPayDeadlineForUser() {
        return payDeadlineForUser;
    }

    public void setPayDeadlineForUser(String payDeadlineForUser) {
        this.payDeadlineForUser = payDeadlineForUser;
    }

    public TarrifsParks() {
        this.formatter = new SimpleDateFormat("EEE d MMM yy", new Locale.Builder().setLanguage("pl").setRegion("PL").build());
        Client_Id = getUserID();
        currentUserProfile = null;
        parksByClient = null;
        tarrifs = null;
        carsTypes = new ArrayList<String>();
        createTarrifMapKeyList();
        operationOptions = createFilterOptionsForOperationType();

    }

    public String getLabelFromValueOperation(String s) {
        String result = "";
        for (int i = 0; i < operationOptions.length; i++) {
            if (s.equals(operationOptions[i].getValue())) {
                result = operationOptions[i].getLabel();
            }
        }
        return result;
    }

    public List<TarrifsModel> getTarrifs() {
        tarrifs = dao.getAllTarrifs();
        map = new HashMap<String, List<TarrifsModel>>();
        for (TarrifsModel tr : tarrifs) {
            String key = tr.getCarType();
            if (map.get(key) == null) {
                map.put(key, new ArrayList<TarrifsModel>());
            }
            map.get(key).add(tr);
        }

        return tarrifs;
    }

    public List<TarrifsModel> getTarrifsFilter(String s) {
        String k = carTypes.Motorcycle.fromLabelToValue(s);
        tarrifs = dao.getAllTarrifs();
        map = new HashMap<String, List<TarrifsModel>>();
        for (TarrifsModel tr : tarrifs) {
            String key = tr.getCarType();
            if (map.get(key) == null) {
                map.put(key, new ArrayList<TarrifsModel>());
            }
            map.get(key).add(tr);
        }

        return map.get(k);
    }

    public List<TarrifsModel> getTarrifsFilter() {
        String k = carTypes.Motorcycle.fromLabelToValue(selectedCarType);
        tarrifs = dao.getAllTarrifs();
        map = new HashMap<String, List<TarrifsModel>>();
        for (TarrifsModel tr : tarrifs) {
            String key = tr.getCarType();
            if (map.get(key) == null) {
                map.put(key, new ArrayList<TarrifsModel>());
            }
            map.get(key).add(tr);
        }

        return map.get(k);
    }

    private SelectItem[] createFilterOptionsForOperationType() {
        SelectItem[] options = new SelectItem[3];

        options[0] = new SelectItem("", "Wybierz");

        options[1] = new SelectItem(Operation.Entry.getOperation(), Operation.Entry.getLabel());
        options[2] = new SelectItem(Operation.Exit.getOperation(), Operation.Exit.getLabel());

        return options;

    }

    private void createTarrifMapKeyList() {
        carsTypes.add("Dostawczy");
        carsTypes.add("Przyczepa");
        carsTypes.add("Motocykl");
        carsTypes.add("Osobowy");
    }

    public Integer getUserID() {
        if (this.lg == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.lg = (LoginBean) app.evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        }
        return this.lg.getId();
    }

    public String getLabelForDurrationTime(TarrifsModel tf) {

        if (tf.getDurationTime().equals("Hour")) {
            return "Godzina";
        }
        if (tf.getDurationTime().equals("Day")) {
            return "Dzień";
        }
        if (tf.getDurationTime().equals("Month")) {
            return "Miesiąc";
        }
        else{
            return "Dwa tygodnie";
        }
    }

}
